package com.popcorn.login.dao;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;








import java.util.Random;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.popcorn.login.model.LoginVO;



public class LoginDAO {

	Connection conn = null;
	PreparedStatement pstmt = null;
	DataSource ds = null;
	ResultSet rs = null;
	
	public LoginDAO(){
	try{
		Context init = new InitialContext();
		ds = (DataSource)init.lookup("java:comp/env/jdbc/Mysql");
		         
	}catch(Exception e){
		e.printStackTrace();
		}	  
	}

	public String isMember(LoginVO vo){
		try{
			conn=ds.getConnection();
			System.out.println("getConnection");
			pstmt=conn.prepareStatement("select * from member where ID=? and PASSWORD=MD5(?)");
			pstmt.setString(1, vo.getId());
			pstmt.setString(2, vo.getPwd());
			rs = pstmt.executeQuery();
			
			System.out.println("=====check=====");
			System.out.println(vo.getId());
			System.out.println(vo.getPwd());
			
			if(rs.next()){ //아이디 비밀번호가 맞으면 1리턴

				return rs.getString("NICKNAME");

			}else{//아이디또는 비밀번호가 틀리면 0리턴

				return null;

			}

		}catch(SQLException se){

			System.out.println(se.getMessage());

			return null; //오류가 발생하면 -1리턴

		}finally{

			try{

				if(rs!=null)rs.close();

				if(pstmt!=null)pstmt.close();

				if(conn!=null)conn.close();

			}catch(SQLException se){}

		}

	}

	public String passwordSearch(String id, String name) {
		try{
			conn=ds.getConnection();
			System.out.println("getConnection");
			pstmt=conn.prepareStatement("select * from member where ID=? and NAME=?");
			pstmt.setString(1, id);
			pstmt.setString(2, name);
			rs = pstmt.executeQuery();
			
			System.out.println("=====email=====");
			System.out.println(id);
			System.out.println(name);
			
			if(rs.next()){ //아이디 비밀번호가 맞으면 1리턴			
				return id;
			}else{//아이디또는 비밀번호가 틀리면 0리턴
				return null;
			}

		}catch(SQLException se){
			System.out.println(se.getMessage());
			return null; //오류가 발생하면 -1리
		}finally{
			try{
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(conn!=null)conn.close();
			}catch(SQLException se){}
		}
	}

	public String randomPassword(String id) {
		try{
			conn=ds.getConnection();
			System.out.println("getConnection");
			pstmt=conn.prepareStatement("UPDATE member SET PASSWORD=MD5(?) WHERE ID=?");
			String pw = random();
			pstmt.setString(1, pw);
			pstmt.setString(2, id);
			pstmt.executeUpdate();
			
			System.out.println("=====PASSWORD=====");
			System.out.println(id);
			System.out.println(pw);
			return pw;
		}catch(SQLException se){
			System.out.println(se.getMessage());
			return null;
		}finally{
			try{
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(conn!=null)conn.close();
			}catch(SQLException se){}
		}
	}

	private String random() {
		// TODO Auto-generated method stub
		String [] ranStr = {"1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
		String pw="";
		for(int i=0;i<6;i++){
			Random ran = new Random();
			pw += ranStr[ran.nextInt(36)];
		}
		return pw;
	}
}